package com.lilosoft.business.precity.service;

import com.lilosoft.business.cga.models.CgaCaseTache;
import com.lilosoft.business.precity.dao.UpProProcessMapper;
import com.lilosoft.business.precity.models.UpProAccept;
import com.lilosoft.business.precity.models.UpProProcess;
import com.lilosoft.business.precity.models.UpProProcessExample;
import com.lilosoft.core.utils.commons.StringUtils;
import com.lilosoft.core.utils.commons.UUIDUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * Created with IntelliJ IDEA.
 * Description: 办理过程信息
 * User: 小峰
 * Date: 2019-08-28
 * Time: 10:18
 */
@Service
public class UpProProcessService {
    @Autowired
    private UpProProcessMapper upProProcessMapper;

    public UpProProcess UpProProcessSynchronous(UpProAccept upProAccept, CgaCaseTache cgaCaseTache) {
        UpProProcess model = new UpProProcess();
        BeanUtils.copyProperties(upProAccept, model);
        model.setRowguid(UUIDUtils.createUUId());
        model.setProjectRowguid(upProAccept.getRowguid());
        model.setProjectno(upProAccept.getProjectno());
        model.setProcessname(cgaCaseTache.getTacheName());
        model.setProcessType(1); //为 2 时特备程序表必须有数据
        model.setDeptCode(upProAccept.getOrgcode());
        model.setDeptName(upProAccept.getOrgname());

        String handleUserId = cgaCaseTache.getOperaterId() == null ? cgaCaseTache.getAssignerIds() : cgaCaseTache.getOperaterId();
        String handleUserName = cgaCaseTache.getOperaterName() == null ? cgaCaseTache.getAssignerNames() : cgaCaseTache.getOperaterName();
        if(handleUserId.contains(",")){
            handleUserId=handleUserId.split(",")[0];
        }
        if(handleUserName.contains(",")){
            handleUserName=handleUserName.split(",")[0];
        }

        model.setHandleuserId(handleUserId);
        model.setHandleusername(handleUserName);
        model.setHandleexplain(cgaCaseTache.getTacheIdea()==null?"默认通过":cgaCaseTache.getTacheIdea());
        model.setEventstarttime(cgaCaseTache.getStartTime());
        model.setEventendtime(cgaCaseTache.getEndTime()==null? new Date(): cgaCaseTache.getEndTime());
        if ( model.getEventstarttime().after(model.getEventendtime())) {
            model.setEventstarttime(model.getEventendtime());
            model.setEventendtime(cgaCaseTache.getStartTime());
        }
        model.setRemark(cgaCaseTache.getId());

        if (StringUtils.isEmpty(cgaCaseTache.getTacheState())) {
            model.setEventname("9");
        } else {
            switch (cgaCaseTache.getTacheState()) {
                case "0":
                    model.setEventname("1");
                    break;
                case "1":
                case "2":
                    model.setEventname("2");
                    break;
                default:
                    model.setEventname("9");
                    break;
            }
        }
        model.setEventDay(StringUtils.toInteger(cgaCaseTache.getTacheDays()));
        Integer mycount = countByRemark(model.getRemark());
        if (null == mycount || mycount <= 0) {
            upProProcessMapper.insertSelective(model);
        }
        return model;
    }

    /**
     * 更新办件的  cgaCaseTache.getId() 的主键 判断数据是否已经在库中
     * @param remark
     * @return
     */
    public Integer countByRemark(String remark) {
        return upProProcessMapper.countByRemark(remark);
    }


    /**
     * 根据cdBatch 统计数据个数（办理过程信息）
     * @param cdBatch
     * @return
     */
    public Long countByCDbatch(String cdBatch) {
        UpProProcessExample example= new UpProProcessExample();
        UpProProcessExample.Criteria criteria = example.createCriteria();
        criteria.andCdBatchEqualTo(cdBatch);
        return upProProcessMapper.countByExample(example);
    }
}
